package com.example.shorg.service.impl;

import com.example.shorg.dto.OrderDTO;
import com.example.shorg.enums.ResultEnum;
import com.example.shorg.exception.ShorgException;
import com.example.shorg.service.BuyerService;
import com.example.shorg.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;
    @Override
    public OrderDTO findOrderOne(String openid, String orderid) {

        OrderDTO orderDTO = checkOrderOwner(openid, orderid);
        return orderDTO;
    }

    private OrderDTO checkOrderOwner(String openid, String orderid) {
        OrderDTO orderDTO = orderService.findOne(orderid);
        if(orderDTO == null){
            return null;
        }
        if(!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid)){
            log.error("【查询订单】");
            throw new ShorgException(ResultEnum.ORDER_STATUS_ERROR);
        }
        return orderDTO;
    }

    @Override
    public OrderDTO cancleOrder(String openid, String orderid) {

        OrderDTO orderDTO = orderService.findOne(orderid);
        if(orderDTO == null){
            log.error("【订单不存在】");
            throw new ShorgException(ResultEnum.ORDER_NOT_EXIST);
        }
        return orderService.cancle(orderDTO);
    }
}
